//
//  WJBaseButton.m
//  WJBSApp
//
//  Created by dllo on 16/7/13.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJBaseButton.h"

@implementation WJBaseButton
//重写button的初始化方法
+(instancetype)buttonWithType:(UIButtonType)buttonType{
    
    [super buttonWithType:buttonType];
    
    WJBaseButton *baseButton = [[WJBaseButton alloc] init];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"] isEqualToString:@"night"]) {
        baseButton.backgroundColor = FIRBLACKCOLOR;
        [baseButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    } else {
        baseButton.backgroundColor = LABLECOLOR;
        [baseButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:baseButton selector:@selector(changeColor:) name:@"changeColor" object:nil];
    
    return [baseButton autorelease];
    
}
+ (instancetype)buttonWithTitle:(NSString *)title
                          frame:(CGRect)frame
                         target:(id)target
                         action:(SEL)action
{

    WJBaseButton *button = [WJBaseButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}


//通知方法
- (void)changeColor:(NSNotification *)notification{
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"] isEqualToString:@"night"]) {
        self.backgroundColor = FIRBLACKCOLOR;
        [self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    } else {
        self.backgroundColor = LABLECOLOR;
        [self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
}

//重写dealloc 删除通知
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeColor" object:nil];
    [super dealloc];
}










@end
